package cn.edu.scnu.service;

import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.entity.PlaybackHistory;
import cn.edu.scnu.mapper.MovieMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


@Service
public class MovieService extends ServiceImpl<MovieMapper, Movie> {

    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    public Movie findMovieById(Integer movieId) {
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("movie_id", movieId);
        return movieMapper.selectOne(queryWrapper);
    }

    // 展示所有电影
    public List<Movie> showAllMovie() {
        QueryWrapper<Movie> queryWrapper = new QueryWrapper();
        List<Movie> movieList = movieMapper.selectList(queryWrapper);
        return movieList;
    }

    // 根据地区查询电影
    public List<Movie> findMovieByArea(String area) {
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
        if (area != null) {
            if (area.equals("内地")) {
                queryWrapper.eq("movie_area", "内地");
            } else if (area.equals("港澳台")) {
                queryWrapper.eq("movie_area", "港澳台");
            } else if (area.equals("韩国")) {
                queryWrapper.eq("movie_area", "韩国");
            } else if (area.equals("日本")) {
                queryWrapper.eq("movie_area", "日本");
            } else if (area.equals("欧美")) {
                queryWrapper.eq("movie_area", "欧美");
            } else if (area.equals("其他区域")) {
                queryWrapper.eq("movie_area", "其他");
            }
        }
        return movieMapper.selectList(queryWrapper);
    }

    // 根据类型查询电影
    public List<Movie> findMovieByType(String type) {
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            if (type.equals("爱情")) {
                queryWrapper.eq("movie_type", "爱情");
            } else if (type.equals("动画")) {
                queryWrapper.eq("movie_type", "动画");
            } else if (type.equals("悬疑")) {
                queryWrapper.eq("movie_type", "悬疑");
            } else if (type.equals("喜剧")) {
                queryWrapper.eq("movie_type", "喜剧");
            } else if (type.equals("动作")) {
                queryWrapper.eq("movie_type", "动作");
            } else if (type.equals("其他类型")) {
                queryWrapper.eq("movie_type", "其他");
            }
        }
        return movieMapper.selectList(queryWrapper);
    }

    // 获取电影链接
    public String getUrlByMovieId(Integer movieId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("movie_id", movieId);
        Movie movie = movieMapper.selectOne(queryWrapper);
        return movie.getMovieUrl();
    }

}
